home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as.sun4 / sparc.c.foobar < prev    next >
Encoding:
Text File  |  1990-01-13  |  26.4 KB  |  1,317 lines

  1. /* sparc.c -- Assemble for the SPARC
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include "sparc-opcode.h"
  25. #include "as.h"
  26. #include "frags.h"
  27. #include "struc-symbol.h"
  28. #include "flonum.h"
  29. #include "expr.h"
  30. #include "hash.h"
  31. #include "md.h"
  32. #include "sparc.h"
  33. #include "write.h"
  34. #include "read.h"
  35. #include "symbols.h"
  36.  
  37. void md_begin();
  38. void md_end();
  39. void md_number_to_chars();
  40. void md_assemble();
  41. char *md_atof();
  42. void md_convert_frag();
  43. void md_create_short_jump();
  44. void md_create_long_jump();
  45. int  md_estimate_size_before_relax();
  46. void md_number_to_imm();
  47. void md_number_to_disp();
  48. void md_number_to_field();
  49. void md_ri_to_chars();
  50. void emit_relocations();
  51. static void sparc_ip();
  52.  
  53. relax_typeS md_relax_table[] = { 0 };
  54.  
  55. /* handle of the OPCODE hash table */
  56. static struct hash_control *op_hash = NULL;
  57.  
  58. static void s_seg(), s_proc(), s_data1(), s_reserve(), s_common();
  59. extern void s_globl(), s_long(), s_short(), s_space(), cons();
  60.  
  61. pseudo_typeS
  62. md_pseudo_table[] = {
  63.     { "common",     s_common,   0 },
  64.     { "global",     s_globl,    0 },
  65.     { "half",       cons,       2 },
  66.     { "proc",       s_proc,     0 },
  67.     { "reserve",    s_reserve,  0 },
  68.     { "seg",        s_seg,      0 },
  69.     { "skip",       s_space,    0 },
  70.     { "word",       cons,       4 },
  71.     { NULL,         0,          0 },
  72. };
  73.  
  74. int md_short_jump_size = 4;
  75. int md_long_jump_size = 4;
  76. int omagic  =  (0x103 << 16) | OMAGIC;  /* Magic number for header */
  77.  
  78. /* This array holds the chars that always start a comment.  If the
  79.     pre-processor is disabled, these aren't very useful */
  80. char comment_chars[] = "!";    /* JF removed '|' from comment_chars */
  81.  
  82. /* This array holds the chars that only start a comment at the beginning of
  83.    a line.  If the line seems to have the form '# 123 filename'
  84.    .line and .file directives will appear in the pre-processed output */
  85. /* Note that input_file.c hand checks for '#' at the beginning of the
  86.    first line of the input file.  This is because the compiler outputs
  87.    #NO_APP at the beginning of its output. */
  88. /* Also note that '/*' will always start a comment */
  89. char line_comment_chars[] = "#";
  90.  
  91. /* Chars that can be used to separate mant from exp in floating point nums */
  92. char EXP_CHARS[] = "eE";
  93.  
  94. /* Chars that mean this number is a floating point constant */
  95. /* As in 0f12.456 */
  96. /* or    0d1.2345e12 */
  97. char FLT_CHARS[] = "rRsSfFdDxXpP";
  98.  
  99. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  100.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  101.    but nothing is ideal around here.
  102.  */
  103. int size_reloc_info = sizeof(struct reloc_info_sparc);
  104.  
  105. static unsigned char octal[256];
  106. #define isoctal(c)  octal[c]
  107. static unsigned char toHex[256];
  108.  
  109. /*
  110.  *  anull bit - causes the branch delay slot instructions to not be executed 
  111.  */
  112. #define ANNUL       (1 << 29)
  113.  
  114. struct sparc_it {
  115.     char    *error;
  116.     unsigned long opcode;
  117.     struct nlist *nlistp;
  118.     expressionS exp;
  119.     int pcrel;
  120.     enum reloc_type reloc;
  121. } the_insn, set_insn;
  122.  
  123. #ifdef __STDC__
  124. static void print_insn(struct sparc_it *insn);
  125. static int getExpression(char *str);
  126. #else
  127. static void print_insn();
  128. static int getExpression();
  129. #endif
  130. static char *expr_end;
  131. static int special_case;
  132.  
  133. #define SPECIAL_CASE_SET    1
  134.  
  135. /*
  136.  * sort of like s_lcomm
  137.  *
  138.  */
  139. static void
  140. s_reserve()
  141. {
  142.     char *name;
  143.     char c;
  144.     char *p;
  145.     int temp;
  146.     symbolS *symbolP;
  147.  
  148.     name = input_line_pointer;
  149.     c = get_symbol_end();
  150.     p = input_line_pointer;
  151.     *p = c;
  152.     SKIP_WHITESPACE();
  153.     if ( * input_line_pointer != ',' ) {
  154.     as_warn("Expected comma after name");
  155.     ignore_rest_of_line();
  156.     return;
  157.     }
  158.     input_line_pointer ++;
  159.     if ((temp = get_absolute_expression()) < 0) {
  160.     as_warn("BSS length (%d.) <0! Ignored.", temp);
  161.     ignore_rest_of_line();
  162.     return;
  163.     }
  164.     *p = 0;
  165.     symbolP = symbol_find_or_make(name);
  166.     *p = c;
  167.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  168.     as_warn("bad .reserve segment: `%s'", input_line_pointer);
  169.     return;
  170.     }
  171.     input_line_pointer += 6;
  172.     if (symbolP->sy_other == 0 
  173.         && symbolP->sy_desc  == 0
  174.     && ((symbolP->sy_type  == N_BSS
  175.     && symbolP->sy_value == local_bss_counter)
  176.     || ((symbolP->sy_type & N_TYPE) == N_UNDF
  177.     && symbolP->sy_value == 0))) {
  178.         symbolP->sy_value = local_bss_counter;
  179.         symbolP->sy_type  = N_BSS;
  180.         symbolP->sy_frag  = & bss_address_frag;
  181.         local_bss_counter += temp;
  182.     } else {
  183.     as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
  184.         symbolP->sy_value, local_bss_counter );
  185.     }
  186.     demand_empty_rest_of_line();
  187.     return;
  188. }
  189.  
  190. static void
  191. s_common()
  192. {
  193.     register char *name;
  194.     register char c;
  195.     register char *p;
  196.     register int temp;
  197.     register symbolS *    symbolP;
  198.  
  199.     name = input_line_pointer;
  200.     c = get_symbol_end();
  201.     /* just after name is now '\0' */
  202.     p = input_line_pointer;
  203.     *p = c;
  204.     SKIP_WHITESPACE();
  205.     if ( * input_line_pointer != ',' ) {
  206.     as_warn("Expected comma after symbol-name");
  207.     ignore_rest_of_line();
  208.     return;
  209.     }
  210.     input_line_pointer ++; /* skip ',' */
  211.     if ( (temp = get_absolute_expression ()) < 0 ) {
  212.     as_warn(".COMMon length (%d.) <0! Ignored.", temp);
  213.     ignore_rest_of_line();
  214.     return;
  215.     }
  216.     *p = 0;
  217.     symbolP = symbol_find_or_make (name);
  218.     *p = c;
  219.     if (   (symbolP->sy_type & N_TYPE) != N_UNDF ||
  220.         symbolP->sy_other != 0 || symbolP->sy_desc != 0) {
  221.     as_warn( "Ignoring attempt to re-define symbol");
  222.     ignore_rest_of_line();
  223.     return;
  224.     }
  225.     if (symbolP->sy_value) {
  226.     if (symbolP->sy_value != temp) {
  227.         as_warn( "Length of .comm \"%s\" is already %d. Not changed to %d.",
  228.         symbolP->sy_name, symbolP->sy_value, temp);
  229.     }
  230.     } else {
  231.     symbolP->sy_value = temp;
  232.     symbolP->sy_type |= N_EXT;
  233.     }
  234.     know(symbolP->sy_frag == &zero_address_frag);
  235.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  236.     p=input_line_pointer;
  237.     while(*p && *p!='\n')
  238.         p++;
  239.     c= *p;
  240.     *p='\0';
  241.     as_warn("bad .common segment: `%s'", input_line_pointer);
  242.     *p=c;
  243.     return;
  244.     }
  245.     input_line_pointer += 6;
  246.     demand_empty_rest_of_line();
  247.     return;
  248. }
  249.  
  250. static void
  251. s_seg()
  252. {
  253.  
  254.     if (strncmp(input_line_pointer, "\"text\"", 6) == 0) {
  255.     input_line_pointer += 6;
  256.     s_text();
  257.     return;
  258.     }
  259.     if (strncmp(input_line_pointer, "\"data\"", 6) == 0) {
  260.     input_line_pointer += 6;
  261.     s_data();
  262.     return;
  263.     }
  264.     if (strncmp(input_line_pointer, "\"data1\"", 7) == 0) {
  265.     input_line_pointer += 7;
  266.     s_data1();
  267.     return;
  268.     }
  269.     as_warn("Unknown segment type");
  270.     demand_empty_rest_of_line();
  271.     return;
  272. }
  273.  
  274. static void
  275. s_data1()
  276. {
  277.     subseg_new(SEG_DATA, 1);
  278.     demand_empty_rest_of_line();
  279.     return;
  280. }
  281.  
  282. static void
  283. s_proc()
  284. {
  285.     extern char is_end_of_line[];
  286.  
  287.     while (!is_end_of_line[*input_line_pointer]) {
  288.     ++input_line_pointer;
  289.     }
  290.     ++input_line_pointer;
  291.     return;
  292. }
  293.  
  294. static void
  295. s_sparc_align()
  296. {
  297.     register unsigned int temp;
  298.     register long int temp_fill;
  299.     unsigned int i;
  300.  
  301.     temp = get_absolute_expression ();
  302. #define MAX_ALIGNMENT (1 << 15)
  303.     if ( temp > MAX_ALIGNMENT ) {
  304.     as_warn("Alignment too large: %d. assumed.", temp = MAX_ALIGNMENT);
  305.     }
  306.  
  307.     /*
  308.      * For the sparc, `.align (1<<n)' actually means `.align n'
  309.      * so we have to convert it.
  310.      */
  311.     if (temp != 0) {
  312.     for (i = 0; (temp & 1) == 0; temp >>= 1, ++i)
  313.         ;
  314.     }
  315.     if (temp != 1) {
  316.     as_warn("Alignment not a power of 2");
  317.     }
  318.     temp = i;
  319.     if (*input_line_pointer == ',') {
  320.     input_line_pointer ++;
  321.     temp_fill = get_absolute_expression ();
  322.     } else {
  323.     temp_fill = 0;
  324.     }
  325.     /* Only make a frag if we HAVE to. . . */
  326.     if (temp && ! need_pass_2) {
  327.     frag_align (temp, (int)temp_fill);
  328.     }
  329.     demand_empty_rest_of_line();
  330.     return;
  331. }
  332.  
  333. /* This function is called once, at assembler startup time.  It should
  334.    set up all the tables, etc. that the MD part of the assembler will need.  */
  335. void
  336. md_begin()
  337. {
  338.   register char *retval = NULL;
  339.   int lose = 0;
  340.   register unsigned int i = 0;
  341.  
  342.   op_hash = hash_new();
  343.   if (op_hash == NULL)
  344.     as_fatal("Virtual memory exhausted");
  345.  
  346.   while (i < NUMOPCODES)
  347.     {
  348.       const char *name = sparc_opcodes[i].name;
  349.       retval = hash_insert(op_hash, name, &sparc_opcodes[i]);
  350.       if(retval != NULL && *retval != '\0')
  351.     {
  352.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  353.            sparc_opcodes[i].name, retval);
  354.       lose = 1;
  355.     }
  356.       do
  357.     {
  358.       if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
  359.         {
  360.           fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
  361.                sparc_opcodes[i].name, sparc_opcodes[i].args);
  362.           lose = 1;
  363.         }
  364.       ++i;
  365.     } while (i < NUMOPCODES
  366.          && !strcmp(sparc_opcodes[i].name, name));
  367.     }
  368.  
  369.   if (lose)
  370.     as_fatal ("Broken assembler.  No assembly attempted.");
  371.  
  372.   for (i = '0'; i < '8'; ++i)
  373.     octal[i] = 1;
  374.   for (i = '0'; i <= '9'; ++i)
  375.     toHex[i] = i - '0';
  376.   for (i = 'a'; i <= 'f'; ++i)
  377.     toHex[i] = i + 10 - 'a';
  378.   for (i = 'A'; i <= 'F'; ++i)
  379.     toHex[i] = i + 10 - 'A';
  380. }
  381.  
  382. void
  383. md_end()
  384. {
  385.     return;
  386. }
  387.  
  388. void
  389. md_assemble(str)
  390.     char *str;
  391. {
  392.     char *toP;
  393.     int rsd;
  394.  
  395.     assert(str);
  396.     sparc_ip(str);
  397.     toP = frag_more(4);
  398.     /* put out the opcode */
  399.     md_number_to_chars(toP, the_insn.opcode, 4);
  400.  
  401.     /* put out the symbol-dependent stuff */
  402.     if (the_insn.reloc != NO_RELOC) {
  403.     fix_new(
  404.         frag_now,                           /* which frag */
  405.         (toP - frag_now->fr_literal), /* where */
  406.         4,                                  /* size */
  407.         the_insn.exp.X_add_symbol,
  408.         the_insn.exp.X_subtract_symbol,
  409.         the_insn.exp.X_add_number,
  410.         the_insn.pcrel,
  411.         the_insn.reloc
  412.     );
  413.     }
  414.     switch (special_case) {
  415.  
  416.     case SPECIAL_CASE_SET:
  417.     special_case = 0;
  418.     assert(the_insn.reloc == RELOC_HI22);
  419.     toP = frag_more(4);
  420.     rsd = (the_insn.opcode >> 25) & 0x1f;
  421.     the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
  422.     md_number_to_chars(toP, the_insn.opcode, 4);
  423.     fix_new(
  424.         frag_now,                           /* which frag */
  425.         (toP - frag_now->fr_literal),       /* where */
  426.         4,                                  /* size */
  427.         the_insn.exp.X_add_symbol,
  428.         the_insn.exp.X_subtract_symbol,
  429.         the_insn.exp.X_add_number,
  430.         the_insn.pcrel,
  431.         RELOC_LO10
  432.     );
  433.     return;
  434.  
  435.     case 0:
  436.     return;
  437.  
  438.     default:
  439.     abort();
  440.     }
  441. }
  442.  
  443. static void
  444. sparc_ip(str)
  445.     char *str;
  446. {
  447.     char *s;
  448.     const char *args;
  449.     char c;
  450.     unsigned long i;
  451.     struct sparc_opcode *insn;
  452.     char *argsStart;
  453.     unsigned long   opcode;
  454.     unsigned int mask;
  455.     int match = FALSE;
  456.     int comma = 0;
  457.  
  458.     for (s = str; islower(*s) || (*s >= '0' && *s <= '3'); ++s)
  459.     ;
  460.     switch (*s) {
  461.  
  462.     case '\0':
  463.     break;
  464.  
  465.     case ',':
  466.     comma = 1;
  467.  
  468.     /*FALLTHROUGH*/
  469.  
  470.     case ' ':
  471.     *s++ = '\0';
  472.     break;
  473.  
  474.     default:
  475.         as_warn("Unknown opcode: `%s'", str);
  476.         exit(1);
  477.     }
  478.     if ((insn = (struct sparc_opcode *) hash_find(op_hash, str)) == NULL) {
  479.     as_warn("`%s' not in hash table.", str);
  480.     return;
  481.     }
  482.     if (comma) {
  483.     *--s = ',';
  484.     }
  485.     argsStart = s;
  486.     for (;;) {
  487.     opcode = insn->match;
  488.     bzero(&the_insn, sizeof(the_insn));
  489.     the_insn.reloc = NO_RELOC;
  490.  
  491.     /*
  492.      * Build the opcode, checking as we go to make
  493.      * sure that the operands match
  494.      */
  495.     for (args = insn->args; ; ++args) {
  496.         switch (*args) {
  497.  
  498.         case '\0':  /* end of args */
  499.         if (*s == '\0') {
  500.             match = TRUE;
  501.         }
  502.         break;
  503.  
  504.         case '+':
  505.         if (*s == '+') {
  506.             ++s;
  507.             continue;
  508.         }
  509.         if (*s == '-') {
  510.             continue;
  511.         }
  512.         break;
  513.  
  514.         case '[':   /* these must match exactly */
  515.         case ']':
  516.         case ',':
  517.         case ' ':
  518.         if (*s++ == *args)
  519.             continue;
  520.         break;
  521.  
  522.         case '#':   /* must be at least one digit */
  523.         if (isdigit(*s++)) {
  524.             while (isdigit(*s)) {
  525.             ++s;
  526.             }
  527.             continue;
  528.         }
  529.         break;
  530.  
  531.         case 'C':   /* coprocessor state register */
  532.         if (strncmp(s, "%csr", 4) == 0) {
  533.             s += 4;
  534.             continue;
  535.         }
  536.         break;
  537.  
  538.         case 'b':    /* next operand is a coprocessor register */
  539.         case 'c':
  540.         case 'D':
  541.             if (*s++ == '%' && *s++ == 'c' && isdigit(*s)) {
  542.             mask = *s++;
  543.             if (isdigit(*s)) {
  544.             mask = 10 * (mask - '0') + (*s++ - '0');
  545.             if (mask >= 32) {
  546.                 break;
  547.             }
  548.             } else {
  549.             mask -= '0';
  550.             }
  551.             switch (*args) {
  552.  
  553.             case 'b':
  554.             opcode |= mask << 14;
  555.             continue;
  556.  
  557.             case 'c':
  558.             opcode |= mask;
  559.             continue;
  560.  
  561.             case 'D':
  562.             opcode |= mask << 25;
  563.             continue;
  564.             }
  565.         }
  566.         break;
  567.  
  568.         case 'r':   /* next operand must be a register */
  569.         case '1':
  570.         case '2':
  571.         case 'd':
  572.         if (*s++ == '%') {
  573.             switch (c = *s++) {
  574.  
  575.             case 'f':   /* frame pointer */
  576.                 if (*s++ == 'p') {
  577.                 mask = 0x1e;
  578.                 break;
  579.             }
  580.             goto error;
  581.  
  582.             case 'g':   /* global register */
  583.             if (isoctal(c = *s++)) {
  584.                 mask = c - '0';
  585.                 break;
  586.             }
  587.             goto error;
  588.  
  589.             case 'i':   /* in register */
  590.             if (isoctal(c = *s++)) {
  591.                 mask = c - '0' + 24;
  592.                 break;
  593.             }
  594.             goto error;
  595.  
  596.             case 'l':   /* local register */
  597.             if (isoctal(c = *s++)) {
  598.                 mask= (c - '0' + 16) ;
  599.                 break;
  600.             }
  601.             goto error;
  602.  
  603.             case 'o':   /* out register */
  604.             if (isoctal(c = *s++)) {
  605.                 mask= (c - '0' + 8) ;
  606.                 break;
  607.             }
  608.             goto error;
  609.  
  610.             case 's':   /* stack pointer */
  611.                 if (*s++ == 'p') {
  612.                 mask= 0xe;
  613.                 break;
  614.             }
  615.             goto error;
  616.  
  617.             case 'r': /* any register */
  618.                 if (!isdigit(c = *s++)) {
  619.                 goto error;
  620.             }
  621.             /* FALLTHROUGH */
  622.             case '0': case '1': case '2': case '3': case '4':
  623.             case '5': case '6': case '7': case '8': case '9':
  624.             if (isdigit(*s)) {
  625.                 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32) {
  626.                 goto error;
  627.                 }
  628.             } else {
  629.                 c -= '0';
  630.             }
  631.             mask= c;
  632.             break;
  633.  
  634.             default:
  635.             goto error;
  636.             }
  637.             /*
  638.              * Got the register, now figure out where
  639.              * it goes in the opcode.
  640.              */
  641.             switch (*args) {
  642.  
  643.             case '1':
  644.             opcode |= mask << 14;
  645.             continue;
  646.  
  647.             case '2':
  648.             opcode |= mask;
  649.             continue;
  650.  
  651.             case 'd':
  652.             opcode |= mask << 25;
  653.             continue;
  654.  
  655.             case 'r':
  656.             opcode |= (mask << 25) | (mask << 14);
  657.             continue;
  658.             }
  659.         }
  660.         break;
  661.  
  662.         case 'e':    /* next operand is a floating point register */
  663.         case 'f':
  664.         case 'g':
  665.             if (*s++ == '%' && *s++ == 'f' && isdigit(*s)) {
  666.             mask = *s++;
  667.             if (isdigit(*s)) {
  668.             mask = 10 * (mask - '0') + (*s++ - '0');
  669.             if (mask >= 32) {
  670.                 break;
  671.             }
  672.             } else {
  673.             mask -= '0';
  674.             }
  675.             switch (*args) {
  676.  
  677.             case 'e':
  678.             opcode |= mask << 14;
  679.             continue;
  680.  
  681.             case 'f':
  682.             opcode |= mask;
  683.             continue;
  684.  
  685.             case 'g':
  686.             opcode |= mask << 25;
  687.             continue;
  688.             }
  689.         }
  690.         break;
  691.  
  692.         case 'F':
  693.         if (strncmp(s, "%fsr", 4) == 0) {
  694.             s += 4;
  695.             continue;
  696.         }
  697.         break;
  698.  
  699.         case 'h':       /* high 22 bits */
  700.             the_insn.reloc = RELOC_HI22;
  701.         goto immediate;
  702.  
  703.         case 'l':   /* 22 bit PC relative immediate */
  704.             the_insn.reloc = RELOC_WDISP22;
  705.         the_insn.pcrel = 1;
  706.         goto immediate;
  707.  
  708.         case 'L':   /* 30 bit immediate */
  709.             the_insn.reloc = RELOC_WDISP30;
  710.         the_insn.pcrel = 1;
  711.         goto immediate;
  712.  
  713.         case 'i':   /* 13 bit immediate */
  714.             the_insn.reloc = RELOC_BASE13;
  715.  
  716.         /*FALLTHROUGH*/
  717.  
  718.         immediate:
  719.         if(*s==' ')
  720.           s++;
  721.         if (*s == '%') {
  722.             if ((c = s[1]) == 'h' && s[2] == 'i') {
  723.             the_insn.reloc = RELOC_HI22;
  724.             s+=3;
  725.             } else if (c == 'l' && s[2] == 'o') {
  726.             the_insn.reloc = RELOC_LO10;
  727.             s+=3;
  728.             } else
  729.                 break;
  730.         }
  731.         /* Note that if the getExpression() fails, we will still have
  732.            created U entries in the symbol table for the 'symbols'
  733.            in the input string.  Try not to create U symbols for
  734.            registers, etc. */
  735.         {
  736.             /* This stuff checks to see if the expression ends
  737.                in +%reg If it does, it removes the register from
  738.                the expression, and re-sets 's' to point to the
  739.                right place */
  740.  
  741.             char *s1;
  742.  
  743.             for(s1=s;*s1 && *s1!=','&& *s1!=']';s1++)
  744.                 ;
  745.  
  746.             if(s1!=s && isdigit(s1[-1])) {
  747.                 if(s1[-2]=='%' && s1[-3]=='+') {
  748.                     s1-=3;
  749.                     *s1='\0';
  750.                     (void)getExpression(s);
  751.                     *s1='+';
  752.                     s=s1;
  753.                     continue;
  754.                 } else if(index("goli0123456789",s1[-2]) && s1[-3]=='%' && s1[-4]=='+') {
  755.                     s1-=4;
  756.                     *s1='\0';
  757.                     (void)getExpression(s);
  758.                     *s1='+';
  759.                     s=s1;
  760.                     continue;
  761.                 }
  762.             }
  763.         }
  764.         (void)getExpression(s);
  765.         s = expr_end;
  766.         continue;
  767.  
  768.         case 'a':
  769.         if (*s++ == 'a') {
  770.             opcode |= ANNUL;
  771.             continue;
  772.         }
  773.         break;
  774.  
  775.         case 'A':       /* alternate space */
  776.         if (isdigit(*s)) {
  777.             long num;
  778.  
  779.             num=0;
  780.             while (isdigit(*s)) {
  781.             num= num*10 + *s-'0';
  782.             ++s;
  783.             }
  784.             opcode |= num<<5;
  785.             continue;
  786.         }
  787.         break;
  788.         /* abort(); */
  789.  
  790.         case 'p':
  791.         if (strncmp(s, "%psr", 4) == 0) {
  792.             s += 4;
  793.             continue;
  794.         }
  795.         break;
  796.  
  797.         case 'q':   /* floating point queue */
  798.         if (strncmp(s, "%fq", 3) == 0) {
  799.             s += 3;
  800.             continue;
  801.         }
  802.         break;
  803.  
  804.         case 'Q':   /* coprocessor queue */
  805.         if (strncmp(s, "%cq", 3) == 0) {
  806.             s += 3;
  807.             continue;
  808.         }
  809.         break;
  810.  
  811.         case 'S':
  812.         if (strcmp(str, "set") == 0) {
  813.             special_case = SPECIAL_CASE_SET;
  814.             continue;
  815.         }
  816.         break;
  817.  
  818.         case 't':
  819.         if (strncmp(s, "%tbr", 4) != 0)
  820.             break;
  821.         s += 4;
  822.         continue;
  823.  
  824.         case 'w':
  825.         if (strncmp(s, "%wim", 4) != 0)
  826.             break;
  827.         s += 4;
  828.         continue;
  829.  
  830.         case 'y':
  831.         if (strncmp(s, "%y", 2) != 0)
  832.             break;
  833.         s += 2;
  834.         continue;
  835.  
  836.         default:
  837.         abort();
  838.         }
  839.         break;
  840.     }
  841.     error:
  842.     if (match == FALSE)
  843.       {
  844.         /* Args don't match.  */
  845.         if (&insn[1] - sparc_opcodes < NUMOPCODES
  846.         && !strcmp(insn->name, insn[1].name))
  847.           {
  848.         ++insn;
  849.         s = argsStart;
  850.         continue;
  851.           }
  852.         else
  853.           {
  854.         as_warn("Illegal operands");
  855.         return;
  856.           }
  857.       }
  858.     break;
  859.     }
  860.  
  861.     the_insn.opcode = opcode;
  862.     return;
  863. }
  864.  
  865. static int
  866. getExpression(str)
  867.     char *str;
  868. {
  869.     char *save_in;
  870.     segT seg;
  871.  
  872.     save_in = input_line_pointer;
  873.     input_line_pointer = str;
  874.     switch (seg = expression(&the_insn.exp)) {
  875.  
  876.     case SEG_ABSOLUTE:
  877.     case SEG_TEXT:
  878.     case SEG_DATA:
  879.     case SEG_BSS:
  880.     case SEG_UNKNOWN:
  881.     case SEG_DIFFERENCE:
  882.     case SEG_BIG:
  883.     case SEG_NONE:
  884.     break;
  885.  
  886.     default:
  887.     the_insn.error = "bad segment";
  888.     expr_end = input_line_pointer;
  889.     input_line_pointer=save_in;
  890.     return 1;
  891.     }
  892.     expr_end = input_line_pointer;
  893.     input_line_pointer = save_in;
  894.     return 0;
  895. }
  896.  
  897.  
  898. #define MAX_LITTLENUMS 6
  899.  
  900. /*
  901.     This is identical to the md_atof in m68k.c.  I think this is right,
  902.     but I'm not sure.
  903.  
  904.    Turn a string in input_line_pointer into a floating point constant of type
  905.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  906.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  907.  */
  908. char *
  909. md_atof(type,litP,sizeP)
  910.     char type;
  911.     char *litP;
  912.     int *sizeP;
  913. {
  914.     int    prec;
  915.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  916.     LITTLENUM_TYPE *wordP;
  917.     char    *t;
  918.     char    *atof_m68k();
  919.  
  920.     switch(type) {
  921.  
  922.     case 'f':
  923.     case 'F':
  924.     case 's':
  925.     case 'S':
  926.     prec = 2;
  927.     break;
  928.  
  929.     case 'd':
  930.     case 'D':
  931.     case 'r':
  932.     case 'R':
  933.     prec = 4;
  934.     break;
  935.  
  936.     case 'x':
  937.     case 'X':
  938.     prec = 6;
  939.     break;
  940.  
  941.     case 'p':
  942.     case 'P':
  943.     prec = 6;
  944.     break;
  945.  
  946.     default:
  947.     *sizeP=0;
  948.     return "Bad call to MD_ATOF()";
  949.     }
  950.     t=atof_m68k(input_line_pointer,type,words);
  951.     if(t)
  952.     input_line_pointer=t;
  953.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  954.     for(wordP=words;prec--;) {
  955.     md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  956.     litP+=sizeof(LITTLENUM_TYPE);
  957.     }
  958.     return "";    /* Someone should teach Dean about null pointers */
  959. }
  960.  
  961. /*
  962.  * Write out big-endian.
  963.  */
  964. void
  965. md_number_to_chars(buf,val,n)
  966.     char *buf;
  967.     long val;
  968.     int n;
  969. {
  970.  
  971.     switch(n) {
  972.  
  973.     case 4:
  974.     *buf++ = val >> 24;
  975.     *buf++ = val >> 16;
  976.     case 2:
  977.     *buf++ = val >> 8;
  978.     case 1:
  979.     *buf = val;
  980.     break;
  981.  
  982.     default:
  983.     abort();
  984.     }
  985.     return;
  986. }
  987.  
  988. void
  989. md_number_to_imm(buf,val,n, fixP, seg_type)
  990.     char *buf;
  991.     long val;
  992.     int n;
  993.     fixS *fixP;
  994.     int seg_type;
  995. {
  996.     if (seg_type != N_TEXT || fixP->fx_r_type == (int) NO_RELOC) {
  997.     switch (n) {
  998.     case 1:
  999.         *buf = val;
  1000.         break;
  1001.     case 2:
  1002.         *buf++ = (val>>8);
  1003.         *buf = val;
  1004.         break;
  1005.     case 4:
  1006.         *buf++ = (val>>24);
  1007.         *buf++ = (val>>16);
  1008.         *buf++ = (val>>8);
  1009.         *buf = val;
  1010.         break;
  1011.     default:
  1012.         abort();
  1013.     }
  1014.     return;
  1015.     }
  1016.  
  1017.     assert(n == 4);
  1018.     assert(fixP->fx_r_type < NO_RELOC);
  1019.  
  1020.     /*
  1021.      * This is a hack.  There should be a better way to
  1022.      * handle this.
  1023.      */
  1024.     if (fixP->fx_r_type == (int) RELOC_WDISP30 && fixP->fx_addsy) {
  1025.         val += fixP->fx_where + fixP->fx_frag->fr_address;
  1026.     }
  1027.  
  1028.     switch (fixP->fx_r_type) {
  1029.  
  1030.     case RELOC_32:
  1031.     buf[0] = val >> 24;
  1032.     buf[1] = val >> 16;
  1033.     buf[2] = val >> 8;
  1034.     buf[3] = val;
  1035.     break;
  1036.  
  1037. #if 0
  1038.     case RELOC_8:         /* These don't seem to ever be needed. */
  1039.     case RELOC_16:
  1040.     case RELOC_DISP8:
  1041.     case RELOC_DISP16:
  1042.     case RELOC_DISP32:
  1043. #endif
  1044.     case RELOC_WDISP30:
  1045.     val = (val >>= 2) + 1;
  1046.     buf[0] |= (val >> 24) & 0x3f;
  1047.     buf[1]= (val >> 16);
  1048.     buf[2] = val >> 8;
  1049.     buf[3] = val;
  1050.     break;
  1051.  
  1052.     case RELOC_HI22:
  1053.     if(!fixP->fx_addsy) {
  1054.       buf[1] |= (val >> 26) & 0x3f;
  1055.       buf[2] = val >> 18;
  1056.       buf[3] = val >> 10;
  1057.     } else {
  1058.       buf[2]=0;
  1059.       buf[3]=0;
  1060.     }
  1061.     break;
  1062. #if 0
  1063.     case RELOC_22:
  1064.     case RELOC_13:
  1065. #endif
  1066.     case RELOC_LO10:
  1067.     if(!fixP->fx_addsy) {
  1068.       buf[2] |= (val >> 8) & 0x03;
  1069.       buf[3] = val;
  1070.     } else
  1071.       buf[3]=0;
  1072.     break;
  1073. #if 0
  1074.     case RELOC_SFA_BASE:
  1075.     case RELOC_SFA_OFF13:
  1076.     case RELOC_BASE10:
  1077. #endif
  1078.     case RELOC_BASE13:
  1079.     buf[2] |= (val >> 8) & 0x1f;
  1080.     buf[3] = val;
  1081.     break;
  1082.  
  1083.     case RELOC_WDISP22:
  1084.     val = (val >>= 2) + 1;
  1085.     /* FALLTHROUGH */
  1086.     case RELOC_BASE22:
  1087.     buf[1] |= (val >> 16) & 0x3f;
  1088.     buf[2] = val >> 8;
  1089.     buf[3] = val;
  1090.     break;
  1091.  
  1092. #if 0
  1093.     case RELOC_PC10: 
  1094.     case RELOC_PC22: 
  1095.     case RELOC_JMP_TBL:
  1096.     case RELOC_SEGOFF16:
  1097.     case RELOC_GLOB_DAT:
  1098.     case RELOC_JMP_SLOT: 
  1099.     case RELOC_RELATIVE:
  1100. #endif
  1101.  
  1102.     case NO_RELOC:
  1103.     default:
  1104.     as_warn("bad relocation type: 0x%02x", fixP->fx_r_type);
  1105.     break;
  1106.     }
  1107.     return;
  1108. }
  1109.  
  1110. /* should never be called for sparc */
  1111. void
  1112. md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1113.     char *ptr;
  1114.     long from_addr, to_addr;
  1115. {
  1116.     fprintf(stderr, "sparc_create_short_jmp\n");
  1117.     abort();
  1118. }
  1119.  
  1120. /* should never be called for sparc */
  1121. void
  1122. md_number_to_disp(buf,val,n)
  1123.     char    *buf;
  1124.     long    val;
  1125. {
  1126.     fprintf(stderr, "md_number_to_disp\n");
  1127.     abort();
  1128. }
  1129.  
  1130. /* should never be called for sparc */
  1131. void
  1132. md_number_to_field(buf,val,fix)
  1133.     char *buf;
  1134.     long val;
  1135. #ifdef __STDC__
  1136.     void *fix;
  1137. #else
  1138.     char *fix;
  1139. #endif
  1140. {
  1141.     fprintf(stderr, "sparc_number_to_field\n");
  1142.     abort();
  1143. }
  1144.  
  1145. void
  1146. md_ri_to_chars(ri_p, ri)
  1147.     struct reloc_info_sparc *ri_p, ri;
  1148. {
  1149.     unsigned long n;
  1150.     char *p;
  1151.  
  1152.     p = (char *) ri_p;
  1153.     n = ri_p->r_address;
  1154.     p[3] = n;
  1155.     p[2] = n >> 8;
  1156.     p[1] = n >> 16;
  1157.     p[0] = n >> 24;
  1158.     n = (ri_p->r_index << 8) + (ri_p->r_extern << 7) + ((int) ri_p->r_type);
  1159.     p[7] = n;
  1160.     p[6] = n >> 8;
  1161.     p[5] = n >> 16;
  1162.     p[4] = n >> 24;
  1163.     n = ri_p->r_addend;
  1164.     p[11] = n;
  1165.     p[10] = n >> 8;
  1166.     p[9] = n >> 16;
  1167.     p[8] = n >> 24;
  1168.     return;
  1169. }
  1170.  
  1171. /* should never be called for sparc */
  1172. void
  1173. md_convert_frag(fragP)
  1174.     register fragS *fragP;
  1175. {
  1176.     fprintf(stderr, "sparc_convert_frag\n");
  1177.     abort();
  1178. }
  1179.  
  1180. /* should never be called for sparc */
  1181. void
  1182. md_create_long_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1183.     char    *ptr;
  1184.     long    from_addr,
  1185.             to_addr;
  1186.     fragS    *frag;
  1187.     symbolS    *to_symbol;
  1188. {
  1189.     fprintf(stderr, "sparc_create_long_jump\n");
  1190.     abort();
  1191. }
  1192.  
  1193. /* should never be called for sparc */
  1194. int
  1195. md_estimate_size_before_relax(fragP, segtype)
  1196.     register fragS *fragP;
  1197. {
  1198.     fprintf(stderr, "sparc_estimate_size_before_relax\n");
  1199.     abort();
  1200.     return 0;
  1201. }
  1202.  
  1203. #if 0
  1204. /* for debugging only */
  1205. static void
  1206. print_insn(insn)
  1207.     struct sparc_it *insn;
  1208. {
  1209.     char *Reloc[] = {
  1210.     "RELOC_8",
  1211.     "RELOC_16",
  1212.     "RELOC_32",
  1213.     "RELOC_DISP8",
  1214.     "RELOC_DISP16",
  1215.     "RELOC_DISP32",
  1216.     "RELOC_WDISP30",
  1217.     "RELOC_WDISP22",
  1218.     "RELOC_HI22",
  1219.     "RELOC_22",
  1220.     "RELOC_13",
  1221.     "RELOC_LO10",
  1222.     "RELOC_SFA_BASE",
  1223.     "RELOC_SFA_OFF13",
  1224.     "RELOC_BASE10",
  1225.     "RELOC_BASE13",
  1226.     "RELOC_BASE22",
  1227.     "RELOC_PC10",
  1228.     "RELOC_PC22",
  1229.     "RELOC_JMP_TBL",
  1230.     "RELOC_SEGOFF16",
  1231.     "RELOC_GLOB_DAT",
  1232.     "RELOC_JMP_SLOT",
  1233.     "RELOC_RELATIVE",
  1234.     "NO_RELOC"
  1235.     };
  1236.  
  1237.     if (insn->error) {
  1238.     fprintf(stderr, "ERROR: %s\n");
  1239.     }
  1240.     fprintf(stderr, "opcode=0x%08x\n", insn->opcode);
  1241.     fprintf(stderr, "reloc = %s\n", Reloc[insn->reloc]);
  1242.     fprintf(stderr, "exp =  {\n");
  1243.     fprintf(stderr, "\t\tX_add_symbol = %s\n",
  1244.     insn->exp.X_add_symbol ?
  1245.     (insn->exp.X_add_symbol->sy_name ? 
  1246.     insn->exp.X_add_symbol->sy_name : "???") : "0");
  1247.     fprintf(stderr, "\t\tX_sub_symbol = %s\n",
  1248.     insn->exp.X_subtract_symbol ?
  1249.         (insn->exp.X_subtract_symbol->sy_name ? 
  1250.             insn->exp.X_subtract_symbol->sy_name : "???") : "0");
  1251.     fprintf(stderr, "\t\tX_add_number = %d\n",
  1252.     insn->exp.X_add_number);
  1253.     fprintf(stderr, "}\n");
  1254.     return;
  1255. }
  1256. #endif
  1257.  
  1258. /*
  1259.  * Sparc relocations are completely different, so it needs
  1260.  * this machine dependent routine to emit them.
  1261.  */
  1262. void
  1263. emit_relocations(fixP, segment_address_in_file)
  1264.     register fixS *fixP;
  1265.     relax_addressT segment_address_in_file;
  1266. {
  1267.     struct reloc_info_sparc ri;
  1268.     register symbolS *symbolP;
  1269.     extern char *next_object_file_charP;
  1270.     long add_number;
  1271.  
  1272.     bzero((char *) &ri, sizeof(ri));
  1273.     for (; fixP; fixP = fixP->fx_next) {
  1274.  
  1275.     if (fixP->fx_r_type >= (int) NO_RELOC) {
  1276.         fprintf(stderr, "fixP->fx_r_type = %d\n", fixP->fx_r_type);
  1277.         abort();
  1278.     }
  1279.  
  1280.     if ((symbolP = fixP->fx_addsy) != NULL) {
  1281.         ri.r_address = fixP->fx_frag->fr_address +
  1282.             fixP->fx_where - segment_address_in_file;
  1283.         if ((symbolP->sy_type & N_TYPE) == N_UNDF) {
  1284.         ri.r_extern = 1;
  1285.         ri.r_index = symbolP->sy_number;
  1286.         } else {
  1287.         ri.r_extern = 0;
  1288.         ri.r_index = symbolP->sy_type & N_TYPE;
  1289.         }
  1290.         if (symbolP && symbolP->sy_frag) {
  1291.         ri.r_addend = symbolP->sy_frag->fr_address;
  1292.         }
  1293.         ri.r_type = (enum reloc_type) fixP->fx_r_type;
  1294.         if (fixP->fx_pcrel) {
  1295. /*        ri.r_addend -= fixP->fx_where;          */
  1296.         ri.r_addend -= ri.r_address;            
  1297.         } else {
  1298.         ri.r_addend = fixP->fx_addnumber;
  1299.         }
  1300.  
  1301. /*        md_ri_to_chars((char *) &ri, ri);        */
  1302.         append(&next_object_file_charP, (char *)& ri, sizeof(ri));
  1303.     }
  1304.     }
  1305.     return;
  1306. }
  1307.  
  1308. int
  1309. md_parse_option(argP,cntP,vecP)
  1310.     char **argP;
  1311.     int *cntP;
  1312.     char ***vecP;
  1313. {
  1314.     return 1;
  1315. }
  1316.  
  1317.